package com.flong.selenium;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.flong.common.IoUtil;

@SuppressWarnings("all")
public class MeiTuanTest2 {

	public static WebDriver driver;

	static {
		System.setProperty("webdriver.ie.driver", "d:/Devsoft/IEDriverServer.exe");
		// driver = new ChromeDriver();
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		ieCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
		driver = new InternetExplorerDriver(ieCapabilities); // 问题3解决方法
		driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);// SECONDS是秒的意思
	}

	public static void main(String[] args) throws IOException, Exception {

        String formPath ="d:/pc_task/sz_1.txt";
		String content="";
		Reader read=null;
		BufferedReader reader = null;
		try {
			read = new InputStreamReader(new FileInputStream(formPath),"UTF-8");
			reader = new BufferedReader(read);
			content = reader.readLine();
			Map<String, String> map = new HashMap<String, String>();
			while (content != null) {
				 content = reader.readLine();
				 if(!"".equals(content)&& content!="null"){

					 driver.navigate().to(content);
					 Thread.sleep(1000);
					 if(isAlertPresent()){
						 driver.switchTo().alert().accept();
					 }
					 WebElement contentInfo = driver.findElement(By.cssSelector("div[class='biz-info__content']"));
					 String shopname = driver.findElement(By.className("biz-info__title")).getText();
					 String address = contentInfo.findElement(By.cssSelector("div[class='biz-item field-group']")).getAttribute("title");
					 String tel = contentInfo.findElement(By.xpath("//div[4][@class='biz-item']")).getText().replace("电话：", "");
					 System.out.println(shopname+"="+address+"="+tel);
				 }
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)
					reader.close();
				if (read != null)
					read.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		// driver.navigate().refresh();
		driver.close();
		driver.quit();
	
	}

	protected static boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }
}